<template>
  <div>
    <div class="head">
      <el-input placeholder="请输入器械名" v-model="searchQuery" style="width: 300px;"></el-input>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </div>
    <el-table :data="tableData">
      <el-table-column prop="name" label="器械名"></el-table-column>
      <el-table-column prop="description" label="器械描述"></el-table-column>
      <el-table-column prop="lastcheck" label="最新检查时间"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="查询结果" :visible.sync="dialogFormVisible">
      <el-table :data="searchResults">
        <el-table-column prop="name" label="器械名"></el-table-column>
        <el-table-column prop="description" label="器械描述"></el-table-column>
        <el-table-column prop="lastcheck" label="最新检查时间"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑器械" :visible.sync="editDialogVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <div class="bookdetail">
          <el-form-item label="器械名字" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <div class="lasttwo">
            <el-form-item label="检查时间" :label-width="formLabelWidth" prop="lastcheck">
              <el-date-picker
                v-model="form.lastcheck"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option label="异常" value="异常"></el-option>
                <el-option label="正常" value="正常"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="lasttwo">
            <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option label="单人器械" value="单人器械"></el-option>
                <el-option label="多人器械" value="多人器械"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
            <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加器械" :visible.sync="addDialogVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <div class="bookdetail">
          <el-form-item label="器械名字" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <div class="lasttwo">
            <el-form-item label="检查时间" :label-width="formLabelWidth" prop="lastcheck">
              <!-- <el-input v-model="form.lastcheck" autocomplete="off"></el-input> -->
              <el-date-picker
                v-model="form.lastcheck"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option label="异常" value="异常"></el-option>
                <el-option label="正常" value="正常"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="lasttwo">
            <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option label="单人器械" value="单人器械"></el-option>
                <el-option label="多人器械" value="多人器械"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
            <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="addBook">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      editDialogVisible: false,
      addDialogVisible: false,
      rules: {
        name: [{ required: true, message: "请输入器械名", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        lastcheck: [
          { required: true, message: "请选择检查日期", trigger: "change" }
        ],
        status: [{ required: true, message: "请输入状态", trigger: "blur" }],
        description: [
          { required: true, message: "请输入描述", trigger: "blur" }
        ]
      },
      tableData: [],
      searchResults: [],
      form: {
        id: "",
        name: "",
        status: "",
        lastcheck: "",
        description: "",
        type: ""
      },
      formLabelWidth: "120px",
      searchQuery: ""
    };
  },
  methods: {
    // formatDate(dateString) {
    //   if (!dateString) return "";
    //   const date = new Date(dateString);
    //   const year = date.getFullYear();
    //   const month = String(date.getMonth() + 1).padStart(2, "0");
    //   const day = String(date.getDate()).padStart(2, "0");
    //   return `${year}-${month}-${day}`;
    // },
    handleEdit(row) {
      this.editDialogVisible = true;
      this.resetForm();
      this.form.id = row.id;
      this.form.lastcheck = row.lastcheck;
      this.form.status = row.status;
      this.form.description = row.description;
      this.form.type = row.type;
      this.form.name = row.name;
      console.log(this.form);
    },

    handleAdd() {
      this.addDialogVisible = true;
      this.resetForm();
    },

    handleDelete(row) {
      // console.log(row.id);
      this.$axios
        .delete(`http://47.108.248.93:999/machine/${row.id}`, {
          headers: { token: localStorage.getItem("token") }
        })
        .then(res => {
          this.$message({
            message: res.data.msg,
            type: "success"
          });
          this.tableData = this.tableData.filter(item => item.id !== row.id);
        });
      this.resetForm();
    },

    handleCancel() {
      this.dialogFormVisible = false;
      this.addDialogVisible = false;
      this.editDialogVisible = false;
      this.resetForm();
    },

    handleSave() {
      this.$refs.form.validate(valid => {
        // console.log(this.form);
        if (valid) {
          // this.form.lastcheck = this.formatDate(this.form.lastcheck);
          this.$axios
            .put("http://47.108.248.93:999/machine", this.form, {
              headers: { token: localStorage.getItem("token") }
            })
            .then(res => {
              this.$message({
                message: res.data.msg,
                type: "success"
              });
              this.editDialogVisible = false;
              this.getAll();
            });
        }
      });
      this.resetForm();
    },
    search() {
      this.searchResults = this.tableData.filter(item =>
        item.name.includes(this.searchQuery)
      );
      this.dialogFormVisible = true;
    },

    addBook() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // this.form.lastcheck = this.formatDate(this.form.lastcheck);
          this.$axios
            .post("http://47.108.248.93:999/machine", this.form, {
              headers: { token: localStorage.getItem("token") }
            })
            .then(res => {
              this.$message({
                message: res.data.msg,
                type: "success"
              });
              this.addDialogVisible = false;
              this.getAll();
            });
        }
      });
    },
    resetForm() {
      this.form = {
        name: "",
        status: "",
        type: "",
        description: "",
        lastcheck: ""
      };
    },
    getAll() {
      const localtoken = localStorage.getItem("token");
      this.$axios
        .get("http://47.108.248.93:999/machine", {
          headers: {
            token: localtoken
          }
        })
        .then(res => {
          this.tableData = res.data.data;
        });
    }
  },
  mounted() {
    const localtoken = localStorage.getItem("token");
    this.$axios
      .get("http://47.108.248.93:999/machine", {
        headers: {
          token: localtoken
        }
      })
      .then(res => {
        this.tableData = res.data.data;
      });
  }
};
</script>

<style scoped>
.head {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.head .el-input {
  margin-right: 10px;
}

.head .el-button {
  margin-right: 10px;
}

.el-table {
  width: 100%;
  margin-bottom: 20px;
}

.el-dialog__body {
  padding-bottom: 0;
}

.bookdetail {
  width: 100%;
}

.bookdetail .el-form-item {
  margin-bottom: 20px;
}

.lasttwo {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  text-align: right;
}
</style>
